"""
题目：给定单链表头节点和目标值，判断链表中是否存在值等于目标值的节点，存在返回 True，否则返回 False。
"""


class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


def contains_target(head, targetValue):
    """判断链表是否包含值为target的节点"""
    current = head
    # 遍历链表，逐个检查节点值
    while current:
        if current.val == targetValue:
            return True  # 找到目标值，返回True
        current = current.next
    # 遍历结束未找到，返回False
    return False


def create_linked_list(arr):
    if not arr:
        return None
    head = ListNode(arr[0])
    current = head
    for val in arr[1:]:
        current.next = ListNode(val)
        current = current.next
    return head


def traverse_linked_list(head):
    result = []
    current = head
    while current:
        result.append(current.val)
        current = current.next
    return result


# 测试
if __name__ == "__main__":
    # 测试1：包含目标值
    head1 = create_linked_list([2, 5, 7, 9])
    print(contains_target(head1, 7))  # 输出: True

    # 测试2：不包含目标值
    head2 = create_linked_list([2, 5, 7, 9])
    print(contains_target(head2, 3))  # 输出: False

    # 测试3：空链表
    head3 = create_linked_list([])
    print(contains_target(head3, 0))  # 输出: False

    # 测试4：单节点包含目标值
    head4 = create_linked_list([6])
    print(contains_target(head4, 6))  # 输出: True